package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OldmanMapper;
import com.csun.cmny.provider.dao.OmcOldManIetypeStashMapper;
import com.csun.cmny.provider.model.domain.OmcOldManIetypeStash;
import com.csun.cmny.provider.model.domain.OmcOldManTypeIetype;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.service.OmcOldManIetypeStashService;
import com.csun.cmny.provider.service.OmcOldManTypeIetypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzheng
 * @description:
 * @date 2019/8/19 13:40
 */
@Slf4j
@Service
@Transactional
public class OmcOldManIetypeStashServiceImpl extends BaseService<OmcOldManIetypeStash> implements OmcOldManIetypeStashService {
    @Resource
    private OldmanMapper oldmanMapper;
    @Resource
    private OmcOldManTypeIetypeService ietypeService;
    @Resource
    private OmcOldManIetypeStashMapper oldManIetypeStashMapper;

    @Override
    public int saveIetypeStash(OmcOldManIetypeStash ietypeStash, LoginAuthDto loginAuthDto) {
        if (null != ietypeStash && null != ietypeStash.getUserTypeId() && ietypeStash.getUserTypeId() > 0 && null != ietypeStash.getOldManId() && ietypeStash.getOldManId() > 0) {
            List<OmcOldManIetypeStash> list = new ArrayList<>();
            Oldman oldmanDto = new Oldman();
            oldmanDto.setHomeId(loginAuthDto.getGroupId().intValue());
            oldmanDto.setOldmanId(ietypeStash.getOldManId().intValue());
            Oldman oldman = oldmanMapper.selectByOld(oldmanDto);
            if (null != oldman) {

                /*
                * 2019-11-18
                * 生成入院缴费单后,老人暂存的减免比例不再更改
                * 下方判断条件 去除 oldman.getStatus = 2 的情况
                * */
//                if (null == oldman.getStatus() || oldman.getStatus() == 0 || oldman.getStatus() == 1 || oldman.getStatus() == 2) {//入院状态  0-未入院 1-待评估 2-待缴费 3-已入院  4-已出院 5-试入住 6-去世
                if (null == oldman.getStatus() || oldman.getStatus() == 0 || oldman.getStatus() == 1) {
                    //已缴入院费之前,减免比例缓存可以修改,根据oldManId先删除之前缓存的,进行再次缓存
                    OmcOldManIetypeStash ietypeDto = new OmcOldManIetypeStash();
                    ietypeDto.setOldManId(ietypeStash.getOldManId());
                    delete(ietypeDto);
                    //查询减免比例
                    OmcOldManTypeIetype typeIetypeDto = new OmcOldManTypeIetype();
                    typeIetypeDto.setGroupId(loginAuthDto.getGroupId());
                    typeIetypeDto.setUserTypeId(ietypeStash.getUserTypeId());
                    List<OmcOldManTypeIetype> dtoList = ietypeService.select(typeIetypeDto);
                    //暂存减免比列
                    if (null != dtoList && dtoList.size() > 0) {
                        for (OmcOldManTypeIetype dto : dtoList) {
                            OmcOldManIetypeStash omcOldManIetypeStash = new OmcOldManIetypeStash();
                            BeanUtils.copyProperties(dto, omcOldManIetypeStash);
                            omcOldManIetypeStash.setId(null);//新增
                            omcOldManIetypeStash.setUpdateInfo(loginAuthDto);
                            omcOldManIetypeStash.setOldManId(ietypeStash.getOldManId());
                            list.add(omcOldManIetypeStash);
                        }
                    }
                }
            }
            return batchSave(list);
        } else {
            return 0;
        }
    }


    public int moveIetypeStash(OmcOldManIetypeStash ietypeStash, LoginAuthDto loginAuthDto) {
        if (null != ietypeStash && null != ietypeStash.getUserTypeId() && ietypeStash.getUserTypeId() > 0 && null != ietypeStash.getOldManId() && ietypeStash.getOldManId() > 0) {
            List<OmcOldManIetypeStash> list = new ArrayList<>();
            Oldman oldmanDto = new Oldman();
            oldmanDto.setHomeId(loginAuthDto.getGroupId().intValue());
            oldmanDto.setOldmanId(ietypeStash.getOldManId().intValue());
            Oldman oldman = oldmanMapper.selectByOld(oldmanDto);
            if (null != oldman) {

                /*
                 * 2019-11-18
                 * 生成入院缴费单后,老人暂存的减免比例不再更改
                 * 下方判断条件 去除 oldman.getStatus = 2 的情况
                 * */
//                if (null == oldman.getStatus() || oldman.getStatus() == 0 || oldman.getStatus() == 1 || oldman.getStatus() == 2) {//入院状态  0-未入院 1-待评估 2-待缴费 3-已入院  4-已出院 5-试入住 6-去世
                if (true) {
                    //已缴入院费之前,减免比例缓存可以修改,根据oldManId先删除之前缓存的,进行再次缓存
                    OmcOldManIetypeStash ietypeDto = new OmcOldManIetypeStash();
                    ietypeDto.setOldManId(ietypeStash.getOldManId());
                    delete(ietypeDto);
                    //查询减免比例
                    OmcOldManTypeIetype typeIetypeDto = new OmcOldManTypeIetype();
                    typeIetypeDto.setGroupId(loginAuthDto.getGroupId());
                    typeIetypeDto.setUserTypeId(ietypeStash.getUserTypeId());
                    List<OmcOldManTypeIetype> dtoList = ietypeService.select(typeIetypeDto);
                    //暂存减免比列
                    if (null != dtoList && dtoList.size() > 0) {
                        for (OmcOldManTypeIetype dto : dtoList) {
                            OmcOldManIetypeStash omcOldManIetypeStash = new OmcOldManIetypeStash();
                            BeanUtils.copyProperties(dto, omcOldManIetypeStash);
                            omcOldManIetypeStash.setId(null);//新增
                            omcOldManIetypeStash.setUpdateInfo(loginAuthDto);
                            omcOldManIetypeStash.setOldManId(ietypeStash.getOldManId());
                            list.add(omcOldManIetypeStash);
                        }
                    }
                }
            }
            return batchSave(list);
        } else {
            return 0;
        }
    }
}
